﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using LinFu.AOP.Interfaces;
using LinFu.IoC.Configuration;
using System.Diagnostics;

namespace CoreEx.Database.Client.Tests
{
    [Implements(typeof(IProfiler<IDbConnection>),LifecycleType.Singleton)]
    public class SampleConnectionProfiler : IProfiler<IDbConnection>
    {
        private  Stopwatch _stopwatch;

        public long TimeElapsed { get; set; }
        
        public void BeforeInvoke(IInvocationInfo info)
        {
            if (info.TargetMethod.Name == "Open")
            {
                _stopwatch = new Stopwatch();
                _stopwatch.Start();
                Thread.Sleep(100);
            }
        }

        public void AfterInvoke(IInvocationInfo info, object returnValue)
        {
            if (info.TargetMethod.Name == "Open")
            {
                _stopwatch.Stop();
                TimeElapsed = _stopwatch.ElapsedMilliseconds;                
            }
        }
    }
}
